package com.yixun.qfbao.api.service.impl;

import com.yixun.qfbao.api.service.DistributionAuditService;
import com.yixun.qfbao.apidto.AddDistributionPremisesReqDto;
import com.yixun.qfbao.apidto.AddDistributionPremisesResDto;
import com.yixun.qfbao.mapper.DistributionAuditMapper;
import com.yixun.qfbao.model.DistributionAudit;
import com.yixun.qfbao.model.dto.DistributionAuditDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.model.enums.PremisesAuditEnum;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

@Service
public class DistributionAuditServiceImpl implements DistributionAuditService {

    @Autowired
    private DistributionAuditMapper mapper;

    /**
     * 分销审核  开放给v10的接口
     * @param dto
     * @return
     */
    @Override
    public AddDistributionPremisesResDto addDistributionAudit(AddDistributionPremisesReqDto dto) {
        AddDistributionPremisesResDto resDto = new AddDistributionPremisesResDto();
        DistributionAudit addModel = new DistributionAudit();
        DistributionAuditDto distributionAuditDto = new DistributionAuditDto();
        distributionAuditDto.setPremisesId(dto.getPremisesId());
        distributionAuditDto.setV10CompanyUUid(dto.getV10CompanyUUid());
        distributionAuditDto.setOrders(" create_time desc ");
        List<DistributionAudit> list = mapper.selectListByDto(distributionAuditDto);

        addModel.setPremisesId(dto.getPremisesId());
        addModel.setV10CompanyUUid(dto.getV10CompanyUUid());
        addModel.setCompanyName(dto.getCompanyName());
        addModel.setOfficial(dto.getOfficial());
        addModel.setOfficialPhone(dto.getOfficialPhone());
        addModel.setAuditStatus(Long.parseLong(PremisesAuditEnum.PRMIISES_AUDITING.getCode()));
        addModel.setDelFlag(DelFlagEnum.UNDEL.getNo());

        int i = 0;
        if(list.size() > 0){
            addModel.setId(list.get(0).getId());
            i = mapper.edit(addModel);
        }else{
            addModel.setCreateTime(Calendar.getInstance().getTime());
            addModel.setApplyTime(Calendar.getInstance().getTime());
            i = mapper.add(addModel);
        }

        if(i > 0){
            resDto.setResult(ResultEnum.SUCCESS.getNo());
        }else{
            resDto.setResult(ResultEnum.FAIL.getNo());
        }
        return resDto;
    }
}
